package com.tanhua.app.controllerservice;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.SoundApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Sound;
import com.tanhua.domain.vo.SoundVo;
import com.tanhua.domain.vo.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author ly
 * @date 2021/9/4 0004 14:19
 */
@Service
public class TaoHuaService {

    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Reference
    private SoundApi soundApi;

    @Reference
    private UserInfoApi userInfoApi;



    /***
     * 桃花传音-发送语音
     * @param soundFile 语音文件
     */
    public void sendVoice(MultipartFile soundFile) throws IOException {
        //获取当前登录用户id
        Long id = UserHolder.getUser().getId();
        //向fastdfs中存储数据
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), FileUtil.extName(soundFile.getOriginalFilename()), null);
        //封装浏览器访问路径
        String soundUrl = fdfsWebServer.getWebServerUrl()+ storePath.getFullPath();
        //创建sound对象
        Sound sound = new Sound();
        sound.setUserId(id);
        sound.setSoundUrl(soundUrl);
        sound.setCreated(System.currentTimeMillis());
        sound.setUpdated(System.currentTimeMillis());
        //调用api保存
        soundApi.save(sound);
    }

    /**
     * 挑花传音接受语音
     * @return SoundVo
     */
    public SoundVo acceptVoice() {
        //获取当前登录用户id
        Long id = UserHolder.getUser().getId();
        //存入redis次数 初始化10 默认1天更新
        Integer times = 10;
        if (stringRedisTemplate.hasKey("User_Sound_number_"+id)){
            times = Integer.parseInt(stringRedisTemplate.opsForValue().get("User_Sound_number_" + id));
        }else {
            stringRedisTemplate.opsForValue().set("User_Sound_number_"+id,times.toString(),1, TimeUnit.DAYS);
        }

        if (times == 0){
            throw new RuntimeException("对不起，您每天的使用次数为10次，您已使用完毕，请明天再来传音");
        }

        List<Sound> list = soundApi.findList(id.intValue());
        if(CollectionUtil.isEmpty(list)){
            throw new RuntimeException("暂时没有其他用户的传音，请稍后再试哦");
        }
        int i = RandomUtil.randomInt(0, list.size());
        Sound sound = list.get(i);
        SoundVo soundVo = new SoundVo();
        UserInfo userInfo = userInfoApi.findUserInfo(sound.getUserId());
        BeanUtil.copyProperties(userInfo,soundVo);
        soundVo.setId(userInfo.getId().intValue());
        soundVo.setSoundUrl(sound.getSoundUrl());
        soundVo.setRemainingTimes(times);

        times = times - 1;
        stringRedisTemplate.opsForValue().set("User_Sound_number_"+id,times.toString(),1, TimeUnit.DAYS);
        soundApi.deleteSound(sound.getId());

        return soundVo;
    }
}
